package com.jinxin.platform.cdsmsmgr.utils;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsmsmgr.pojo.entity.*;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @Author 那个男人
 * @Date 2022/12/30 17:14
 * @Version 1.0
 */
@Slf4j
public class TxSmsUtil{
    //发送短信
    public static JsonResult sendSms(TxSmsSendParam sendParam){
        String secretId = sendParam.getSecretId();
        String secretKey = sendParam.getSecretKey();
        try{
            SmsClient client = getSmsClient(secretId, secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(sendParam.getPhoneNumberSet());
            String[] templateParamSet = sendParam.getTemplateParamSet();
            if(templateParamSet != null && templateParamSet.length > 0){
                req.setTemplateParamSet(templateParamSet);
            }
            req.setSmsSdkAppId(sendParam.getSmsSdkAppId());
            req.setTemplateId(sendParam.getTemplateId());
            req.setSignName(sendParam.getSignName());
            //req.setSmsSdkAppid(sendParam.getSmsSdkAppId());
            //req.setTemplateID(sendParam.getTemplateId());
            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            log.info("腾讯发送短信: " + SendSmsResponse.toJsonString(resp));
            SendStatus[] sendStatusSet = resp.getSendStatusSet();
            int okCount = 0,errorCount = 0;
            for(SendStatus sendStatus : sendStatusSet){
                if(Constant.STRING_OK.equals(sendStatus.getCode())){
                    okCount ++;
                    continue;
                }
                errorCount++;
            }
            return JsonResult.ok("成功发送"+okCount+"短信,失败"+errorCount+"条");
            // 输出json格式的字符串回包
            //SendSmsResponse.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
            return JsonResult.error("发送短信异常");
        }
    }
    //添加签名
    public static JsonResult addSmsSign(TxAddSmsSignParam addSmsSignParam){
        String secretId = addSmsSignParam.getSecretId();
        String secretKey = addSmsSignParam.getSecretKey();
        try{
            SmsClient client = getSmsClient(secretId, secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            AddSmsSignRequest req = new AddSmsSignRequest();
            req.setSignName(addSmsSignParam.getSignName());
            req.setSignType(Long.valueOf(addSmsSignParam.getSignType()));
            req.setDocumentType(Long.valueOf(addSmsSignParam.getDocumentType()));
            req.setInternational(Long.valueOf(addSmsSignParam.getInternational()));
            req.setSignPurpose(Long.valueOf(addSmsSignParam.getSignPurpose()));
            //req.setUsedMethod(Long.valueOf(addSmsSignParam.getSignPurpose()));
            req.setProofImage(addSmsSignParam.getProofImage());
            req.setRemark(addSmsSignParam.getRemark());
            // 返回的resp是一个AddSmsSignResponse的实例，与请求对象对应
            AddSmsSignResponse resp = client.AddSmsSign(req);
            log.info("腾讯添加签名: " + AddSmsSignResponse.toJsonString(resp));
            AddSignStatus addSignStatus = resp.getAddSignStatus();
            if(addSignStatus != null && !StringUtils.isEmpty(addSignStatus.getSignId())){
                return JsonResult.ok("申请短信签名成功",addSignStatus.getSignId());
            }
            return JsonResult.error("申请短信签名失败");
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
            return JsonResult.error("申请短信签名异常: " + e.toString());
        }
    }

    //短信签名状态查询
    public static JsonResult describeSmsSignList(TxSmsSignQueryParam smsSignQueryParam){
        String secretId = smsSignQueryParam.getSecretId();
        String secretKey = smsSignQueryParam.getSecretKey();
        try{
            SmsClient client = getSmsClient(secretId, secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeSmsSignListRequest req = new DescribeSmsSignListRequest();
            req.setSignIdSet(smsSignQueryParam.getSignIdSet());
            req.setInternational(Long.valueOf(smsSignQueryParam.getInternational()));
            // 返回的resp是一个DescribeSmsSignListResponse的实例，与请求对象对应
            DescribeSmsSignListResponse resp = client.DescribeSmsSignList(req);
            log.info("腾讯短信签名状态查询: " + DescribeSmsSignListResponse.toJsonString(resp));
            return JsonResult.ok("签名状态查询成功",resp.getDescribeSignListStatusSet());
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
            return JsonResult.error("短信签名状态查询异常: " + e.getMessage());
        }
    }

    //修改签名
    public static JsonResult modifySmsSign(TxModifySmsSignParam modifySmsSignParam){
        String secretId = modifySmsSignParam.getSecretId();
        String secretKey = modifySmsSignParam.getSecretKey();
        try{
            SmsClient client = getSmsClient(secretId, secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            ModifySmsSignRequest req = new ModifySmsSignRequest();
            req.setSignId(Long.valueOf(modifySmsSignParam.getSignId()));
            req.setSignName(modifySmsSignParam.getSignName());
            req.setSignType(Long.valueOf(modifySmsSignParam.getSignType()));
            req.setDocumentType(Long.valueOf(modifySmsSignParam.getDocumentType()));
            req.setInternational(Long.valueOf(modifySmsSignParam.getInternational()));
            req.setSignPurpose(Long.valueOf(modifySmsSignParam.getSignPurpose()));
            //req.setUsedMethod(Long.valueOf(modifySmsSignParam.getSignPurpose()));
            req.setProofImage(modifySmsSignParam.getProofImage());
            req.setRemark(modifySmsSignParam.getRemark());
            // 返回的resp是一个AddSmsSignResponse的实例，与请求对象对应
            ModifySmsSignResponse resp = client.ModifySmsSign(req);
            log.info("腾讯修改短信签名: " + ModifySmsSignResponse.toJsonString(resp));
            ModifySignStatus modifySignStatus = resp.getModifySignStatus();
            if(modifySignStatus != null && !StringUtils.isEmpty(modifySignStatus.getSignId())){
                return JsonResult.ok("修改短信签名成功",modifySignStatus.getSignId());
            }
            return JsonResult.error("修改短信签名失败");
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
            return JsonResult.error("修改短信签名异常: " + e.getMessage());
        }
    }
    // 添加短信模板
    public static JsonResult addSmsTemplate(TxAddSmsTemplateParam addSmsTemplateParam){
        String secretId = addSmsTemplateParam.getSecretId();
        String secretKey = addSmsTemplateParam.getSecretKey();
        try{
            SmsClient client = getSmsClient(secretId,secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            AddSmsTemplateRequest req = new AddSmsTemplateRequest();
            req.setTemplateName(addSmsTemplateParam.getTemplateName());
            req.setTemplateContent(addSmsTemplateParam.getTemplateContent());
            req.setSmsType(Long.valueOf(addSmsTemplateParam.getSmsType()));
            req.setInternational(Long.valueOf(addSmsTemplateParam.getInternational()));
            req.setRemark(addSmsTemplateParam.getRemark());
            // 返回的resp是一个AddSmsTemplateResponse的实例，与请求对象对应
            AddSmsTemplateResponse resp = client.AddSmsTemplate(req);
            log.info("腾讯添加短信模板: " + AddSmsTemplateResponse.toJsonString(resp));
            AddTemplateStatus addTemplateStatus = resp.getAddTemplateStatus();
            if(addTemplateStatus != null && !StringUtils.isEmpty(addTemplateStatus.getTemplateId())){
                return JsonResult.ok("申请短信模板成功",addTemplateStatus.getTemplateId());
            }
            return JsonResult.error("申请短信模板失败");
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
            return JsonResult.error("申请短信模板异常: " + e.getMessage());
        }
    }
    // 短信模板状态查询
    public static JsonResult describeSmsTemplateList(TxSmsTemplateQueryParam smsTemplateQueryParam){
        String secretId = smsTemplateQueryParam.getSecretId();
        String secretKey = smsTemplateQueryParam.getSecretKey();
        try{
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = getSmsClient(secretId,secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeSmsTemplateListRequest req = new DescribeSmsTemplateListRequest();
            Long[] templateIdSet = smsTemplateQueryParam.getTemplateIdSet();
            if(templateIdSet != null && templateIdSet.length > 0){
                req.setTemplateIdSet(templateIdSet);
            }else {
                req.setLimit(Long.valueOf(smsTemplateQueryParam.getLimit()));
                req.setOffset(Long.valueOf(smsTemplateQueryParam.getOffset()));
            }
            req.setInternational(Long.valueOf(smsTemplateQueryParam.getInternational()));
            // 返回的resp是一个DescribeSmsTemplateListResponse的实例，与请求对象对应
            DescribeSmsTemplateListResponse resp = client.DescribeSmsTemplateList(req);
            log.info("腾讯短信模板状态查询: " + DescribeSmsTemplateListResponse.toJsonString(resp));
            return JsonResult.ok("success",resp.getDescribeTemplateStatusSet());
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
            return JsonResult.error("短信模板状态查询异常:" + e.getMessage());
        }
    }
    // 修改短信模板
    public static JsonResult modifySmsTemplate(TxModifySmsTemplateParam modifySmsTemplateParam){
        String secretId = modifySmsTemplateParam.getSecretId();
        String secretKey = modifySmsTemplateParam.getSecretKey();
        try{
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = getSmsClient(secretId,secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            ModifySmsTemplateRequest req = new ModifySmsTemplateRequest();
            req.setTemplateId(Long.valueOf(modifySmsTemplateParam.getTemplateId()));
            req.setTemplateName(modifySmsTemplateParam.getTemplateName());
            req.setTemplateContent(modifySmsTemplateParam.getTemplateContent());
            req.setSmsType(Long.valueOf(modifySmsTemplateParam.getSmsType()));
            req.setInternational(Long.valueOf(modifySmsTemplateParam.getInternational()));
            req.setRemark(modifySmsTemplateParam.getRemark());
            // 返回的resp是一个ModifySmsTemplateResponse的实例，与请求对象对应
            ModifySmsTemplateResponse resp = client.ModifySmsTemplate(req);
            log.info("腾讯修改短信模板: " + ModifySmsTemplateResponse.toJsonString(resp));
            ModifyTemplateStatus modifyTemplateStatus = resp.getModifyTemplateStatus();
            if(modifyTemplateStatus != null && !StringUtils.isEmpty(modifyTemplateStatus.getTemplateId())){
                return JsonResult.ok("修改短信模板成功",modifyTemplateStatus.getTemplateId());
            }
            return JsonResult.error("修改短信模板失败");
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
            return JsonResult.error("修改短信模板异常: " + e.getMessage());
        }
    }

    private static SmsClient getSmsClient(String secretId, String secretKey) {
        // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
        // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(Constant.TX_END_POINT);
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        return new SmsClient(cred, Constant.DEFAULT_REGION, clientProfile);
    }

}
